
package net.swa.dewei.thread;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import net.sf.json.JSONObject;
import net.swa.dewei.beans.entity.WeixinInfo;
import net.swa.dewei.service.WeixinInfoService;
import net.swa.util.BuzConst;
import net.swa.util.FileUtil;
import net.swa.util.MySecureProtocolSocketFactory;
import net.swa.util.StringUtil;

/***
 * 查询微信详细信息操作类
 * @author dawei
 *
 */
public class QueryWxInfoThread extends Thread
{
    private final Logger log = Logger.getLogger(QueryWxInfoThread.class);

    private static List<Thread> runningThreads = new ArrayList<Thread>();

    private static int c;

    private static int total;

    //    static List<WeixinInfo> list = new ArrayList<WeixinInfo>();

    private String token;

    private String openId;

    private WeixinInfoService weixinInfoService;

	@SuppressWarnings("static-access")
	public QueryWxInfoThread(String token, String openId, WeixinInfoService weixinInfoService, int total)
    {
        if (log.isDebugEnabled())
        {
            log.debug("创建线程");
        }
        this.token = token;
        this.openId = openId;
        this.weixinInfoService = weixinInfoService;
        this.total = total;
    }

    public void run()
    {
        regist(this);//线程开始时注册
        try
        {
            work();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        unRegist(this);
    }

    public void work() throws Exception
    {
        if (log.isDebugEnabled())
        {
            log.debug("线程开始执行。。。线程名：" + Thread.currentThread().getName());
        }
        WeixinInfo wx = new WeixinInfo();
        wx.setId(openId);
        String nickname = null;
        String http = BuzConst.HTTP_WX_USER_INFO+"?access_token=" + token + "&openid=" + openId;
        String resp;
        resp = MySecureProtocolSocketFactory.get(http);
        JSONObject jsonObj = JSONObject.fromObject(resp.toString());
        if (null != jsonObj)
        {
            if (null != jsonObj.get("nickname"))
            {
                nickname = jsonObj.get("nickname").toString();
                nickname = StringUtil.filterStr(nickname);
                wx.setNickname(nickname);
            }
            if (null != jsonObj.get("province"))
            {
                String province = jsonObj.get("province").toString();

                wx.setProvince(province);
            }

            if (null != jsonObj.get("city"))
            {
                String city = jsonObj.get("city").toString();
                wx.setCity(city);
            }

            if (null != jsonObj.get("headimgurl"))
            {
                String headimgurl = jsonObj.get("headimgurl").toString();
                wx.setHeadimgurl(headimgurl);
                if (headimgurl.length() > 0)
                {
                    wx.setSheadimgurl("data:image/png;base64," + FileUtil.imageToBase64(headimgurl.substring(0, headimgurl.length() - 1) + "64"));
                }
            }

            if (null != jsonObj.get("subscribe"))
            {
                String subscribe = jsonObj.get("subscribe").toString();
                wx.setSubscribe(subscribe);
            }

            if (null != jsonObj.get("sex"))
            {
                String sex = jsonObj.get("sex").toString();
                wx.setSex(sex);
            }

            if (null != jsonObj.get("language"))
            {
                String language = jsonObj.get("language").toString();
                wx.setLanguage(language);
            }

            if (null != jsonObj.get("country"))
            {
                String country = jsonObj.get("country").toString();
                wx.setCountry(country);
            }
            if (null != jsonObj.get("subscribe_time"))
            {
                String subscribe_time = jsonObj.get("subscribe_time").toString();
                wx.setSubscribe_time(subscribe_time);
            }
        }
        wx.setStatus(BuzConst.WX_GUANZHU);
        weixinInfoService.saveOrUpdateByManu(wx);
    }

    public void regist(Thread t)
    {
        synchronized (runningThreads)
        {
            runningThreads.add(t);
        }
    }

    public void unRegist(Thread t)
    {
        synchronized (runningThreads)
        {
            runningThreads.remove(t);
            c++;
        }
    }

    public static boolean hasThreadRunning()
    {
        return (runningThreads.size() > 0);//通过判断runningThreads是否为空就能知道是否还有线程未执行完  
    }

    public String getToken()
    {
        return token;
    }

    public void setToken(String token)
    {
        this.token = token;
    }

    public String getOpenId()
    {
        return openId;
    }

    public void setOpenId(String openId)
    {
        this.openId = openId;
    }

    public WeixinInfoService getWeixinInfoService()
    {
        return weixinInfoService;
    }

    public void setWeixinInfoService(WeixinInfoService weixinInfoService)
    {
        this.weixinInfoService = weixinInfoService;
    }

    public static int getC()
    {
        return c;
    }

    public static void setC(int c)
    {
        QueryWxInfoThread.c = c;
    }

    public static int getProcess()
    {
        return 100 * c / total;
    }

}
